package ema.jee.tp.client;

import java.rmi.RMISecurityManager;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;

import javax.naming.InitialContext;

import ema.jee.tp.Application;
import ema.jee.tp.Bank;


public class BankUnitTest {

	public static void displayBanks(Application app) {
		Collection<Bank> banks = app.listBank();
		for (Iterator<Bank> i = banks.iterator(); i.hasNext();) {
			Bank b = i.next();
			System.out.println(b.toString());
		}
	}	
	
	private static Random rand = new Random();	
	
	public static void insertBank(Application app) {
		Bank b = new Bank();
		b.setName("Banque "+RandomString.generateStringAlpha(10));
		b.setAddress(rand.nextInt(100)+" rue des "+RandomString.generateStringAlpha(10));
		b.setPhone(rand.nextInt(100)+"."+rand.nextInt(100)+"."+rand.nextInt(100)+"."+rand.nextInt(100)+"."+rand.nextInt(100));
		b.setZipCode(30000+rand.nextInt(1000));
        app.addBank(b);
	}
	
	public static void main(String[] args) {
		if (System.getSecurityManager() == null) {
			System.setProperty("java.security.policy", "java.policy");
			System.setSecurityManager(new RMISecurityManager());
		}

		Application app = EjbLocator.getInstance().getEjbRemote("myeap","ApplicationBean");
		
		/*
		try {
			app = (Application) new InitialContext().lookup("bankEAR/ApplicationBean/remote");
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
		
		for (int i=0;i<10;i++)
			  insertBank(app);
		displayBanks(app);
		System.exit(0);
	}

}
